package com.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.junit.Test;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

public class image {

//    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
//        QRCodeWriter qrCodeWriter = new QRCodeWriter();
//
//        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//
//        Path path = FileSystems.getDefault().getPath(filePath);
//
//        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
//    }
//    public static void generate(Map<String,String> maps){
//        System.out.println("生成");
//        String QR_CODE_IMAGE_PATH = "src/main/resources/statics"+"/"+maps.get("openid")+".png";
//        try {
//            generateQRCodeImage(maps.toString(), 350, 350, QR_CODE_IMAGE_PATH);
////            generateQRCodeImage("1");
//        } catch (WriterException e) {
//            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
//        } catch (IOException e) {
//            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
//        }
//
//    }
    public String filePath1 = "";
    @Test
    public void t() throws IOException, WriterException {
        Map maps = new HashMap();
        maps.put("openid","ojMPn5Xn0hdxKd56S4s-35dv4z5w");
        maps.put("id_number",1);
        maps.put("phone",1);
        maps.put("room_id",1);
        maps.put("name",1);
        maps.put("year",1);
        maps.put("health",1);
        maps.put("sex",1);
        generateQRCodeImage(maps,200,200);
    }
    public String generateQRCodeImage(Map<String, String> map, int width, int height) throws WriterException, IOException {
        File path1 = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!path1.exists()) {
            path1 = new File("");
        }
        System.out.println(path1.getAbsolutePath());
        File upload = new File(path1.getAbsolutePath(),"static/upload/");
        if(!upload.exists()) {
            upload.mkdirs();
        }
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        String  filePath = upload.getAbsolutePath()+"\\"+map.get("openid")+".png";
        System.out.println(filePath);
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
            System.out.println("删除成功");
        }else {
            file.mkdir();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("openid",map.get("openid"));
        jsonObject.put("id_number",map.get("id_number"));
        jsonObject.put("phone",map.get("phone"));
        jsonObject.put("room_id",map.get("room_id"));
        jsonObject.put("name",map.get("name"));
        jsonObject.put("year",map.get("year"));
        jsonObject.put("sex",map.get("sex"));
        jsonObject.put("health",map.get("health"));
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

        BitMatrix bitMatrix =qrCodeWriter.encode(jsonObject.toJSONString(),BarcodeFormat.QR_CODE, width, height,hints);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        InputStream ins = getClass().getClassLoader().getResourceAsStream(filePath);
        return filePath;
    }


}
